package com.sophialex.health.record.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.DateTime;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.util.TimeHelper;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.Assert;
import com.sophialex.health.system.frame.util.RefMethod;

public class PublicConstants {
	public static final Color black_COLOR = RcpResourceManager
			.getColor("0, 0, 0");// black黑色
	public static final Color white_COLOR = RcpResourceManager
			.getColor("255, 255, 255");// 白色
	public static final Color blue_COLOR = RcpResourceManager
			.getColor("0, 0, 255");// 蓝色
	public static final Color green_COLOR = RcpResourceManager
			.getColor("0, 128, 128");// 绿色
	public static final Color remark_COLOR = RcpResourceManager
			.getColor("238, 241, 245");
	public static final Color ChargeLeft_COLOR = RcpResourceManager
			.getColor("228, 228, 240");
	public static final Color ChargeMain_COLOR = RcpResourceManager
			.getColor("217, 220, 226");
	public static final Color chargQueryBackground = RcpResourceManager
			.getColor("228, 234, 241");
	public static final Color reportBackground = RcpResourceManager.getColor(
			240, 240, 245);
	public static Color chargPayInfoBackground = green_COLOR;
	public static Color red_COLOR = RcpResourceManager.getColor("255, 0, 0");// 红色

	public static final Color COLOR1 = RcpResourceManager.getColor(237, 237,
			245);

	public static final Color COLOR = RcpResourceManager
			.getColor(238, 238, 247);

	public static final SimpleDateFormat sf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static final SimpleDateFormat sf1 = new SimpleDateFormat(
			"yyyy-MM-dd");

	public final static int Table_Row_Height = 20; // 行高

	public final static int Table_Head_Height = 28;// 表头高度

	/**
	 * 获取对象的属性值
	 * 
	 * @param obj
	 *            对象
	 * @param fieldName
	 *            属性名
	 * @return
	 */
	public static Object getProperty(Object obj, String fieldName) {
		if (obj == null || fieldName == null)
			return "";

		Class cls = obj.getClass();

		Field fieldlist[] = cls.getDeclaredFields();

		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < fieldlist.length; i++) {
			Field fld = fieldlist[i];
			String fName = fld.getName();
			map.put(fName.toLowerCase(), fName);
		}

		Object temp = null;

		if (!map.containsKey(fieldName)) {
			return temp;
		}

		try {
			Field field = cls.getDeclaredField(fieldName);
			field.setAccessible(true);
			temp = field.get(obj);

			// if (field.getType().equals(java.util.Date.class) && temp != null
			// && !temp.equals("")) {
			// temp = TimeHelper.dateToString((Date) temp,
			// "yyyy-MM-dd HH:mm:ss");
			// }
		} catch (Exception e) {
			temp = "";
			SuperContext.showExceptionMsg(e);
		}
		return temp;
	}

	/**
	 * 设置对象的属性值
	 * 
	 * @param obj
	 *            对象
	 * @param fieldName
	 *            属性名
	 * @param fieldValue
	 *            属性值
	 */
	public static void setProperty(Object obj, String fieldName,
			Object fieldValue) {
		if (obj == null || fieldName == null || fieldValue == null)
			return;

		Class cls = obj.getClass();

		Field fieldlist[] = cls.getDeclaredFields();

		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < fieldlist.length; i++) {
			Field fld = fieldlist[i];
			String fName = fld.getName();
			map.put(fName.toLowerCase(), fName);
		}

		if (!map.containsKey(fieldName)) {

		}

		try {
			Field field = cls.getDeclaredField(fieldName);
			field.setAccessible(true);
			Class fieldType = field.getType();

			String methodName = "set";

			methodName += fieldName.toUpperCase().substring(0, 1);
			methodName += fieldName.substring(1, fieldName.length());

			Method method = cls.getDeclaredMethod(methodName, fieldType);

			if (fieldType.equals(Boolean.class)
					|| fieldType.equals(boolean.class)) {
				method.invoke(obj, Boolean.parseBoolean(fieldValue.toString()));
			} else if (fieldType.equals(Byte.class)
					|| fieldType.equals(byte.class)) {
				method.invoke(obj, Byte.parseByte(fieldValue.toString()));
			} else if (fieldType.equals(Short.class)
					|| fieldType.equals(short.class)) {
				method.invoke(obj, Short.parseShort(fieldValue.toString()));
			} else if (fieldType.equals(Integer.class)
					|| fieldType.equals(int.class)) {
				method.invoke(obj, Integer.parseInt(fieldValue.toString()));
			} else if (fieldType.equals(Long.class)
					|| fieldType.equals(long.class)) {
				method.invoke(obj, Long.parseLong(fieldValue.toString()));
			} else if (fieldType.equals(Float.class)
					|| fieldType.equals(float.class)) {
				method.invoke(obj, Float.parseFloat(fieldValue.toString()));
			} else if (fieldType.equals(Double.class)
					|| fieldType.equals(double.class)) {
				method.invoke(obj, Double.parseDouble(fieldValue.toString()));
			} else if (fieldType.equals(Date.class)) {
				Date date = null;
				if (fieldValue != null) {
					String fieldValue1 = fieldValue.toString().trim();
					fieldValue1 = fieldValue.toString().trim()
							.replace("-", "/");
					fieldValue1 = fieldValue1.toString().trim().replace("—",
							"/");
					fieldValue1 = fieldValue1.toString().trim().replace(".",
							"/");
					date = TimeHelper.stringToTime(fieldValue1,
							"yyyy/MM/dd HH:mm:ss");
					if (date == null) {
						date = TimeHelper.stringToTime(fieldValue1,
								"yyyyMMdd HH:mm:ss");
						if (date == null) {
							try {
								date = (Date) fieldValue;
							} catch (Exception e) {
							}
						}
					}

				}
				method.invoke(obj, date);
			} else {
				method.invoke(obj, fieldType.cast(fieldValue));
			}

		} catch (Exception e) {
			// SuperContext.showExceptionMsg(e);
		}
	}

	/*
	 * 给DateTime日期控件附值
	 */
	public static void setDate(DateTime dateTime, Date date) {
		if (date == null)
			return;

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		dateTime.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
				.get(Calendar.DAY_OF_MONTH));
		dateTime.redraw();
	}

	/*
	 * 从DateTime日期控件取得值 格式为：yyyy-MM-dd
	 */
	public static Date getDate(DateTime dateTime) {
		String value = dateTime.getYear()
				+ "-"
				+ ((dateTime.getMonth() + 1) > 9 ? (dateTime.getMonth() + 1)
						: "0" + (dateTime.getMonth() + 1))
				+ "-"
				+ (dateTime.getDay() > 9 ? dateTime.getDay() : "0"
						+ dateTime.getDay());

		return TimeHelper.stringToTime(value, "yyyy-MM-dd");
	}

	/*
	 * 从DateTime日期控件取得值 格式为：yyyy-MM-dd
	 */
	public static String getDateString(DateTime dateTime) {
		String value = dateTime.getYear()
				+ "-"
				+ ((dateTime.getMonth() + 1) > 9 ? (dateTime.getMonth() + 1)
						: "0" + (dateTime.getMonth() + 1))
				+ "-"
				+ (dateTime.getDay() > 9 ? dateTime.getDay() : "0"
						+ dateTime.getDay());

		return value;
	}

	/**
	 * 精确匹配和模糊匹配放一起，先左精确完，后模糊匹配
	 * 
	 * @param <T>
	 * @param list
	 * @param firstProperties
	 * @param text
	 * @return
	 */
	public static <T> List<T> exactFilter(List<T> list,
			String[] firstProperties, String text) {
		if (firstProperties == null)
			return null;
		List<T> list1 = new ArrayList<T>();
		List<T> list2 = new ArrayList<T>();
		if (Assert.isNull(text))
			return list;
		if (Assert.isNull(list))
			return list;
		try {
			int idx = -1;
			T obj = null;
			for (T t : list) {
				obj = null;
				// if(l.contains(t))continue;
				Object o;
				for (String s : firstProperties) {
					o = null;
					if (s.contains(".")) {
						StringTokenizer st = new StringTokenizer(s, ".", false);
						o = t;
						while (st.hasMoreElements()) {
							String str = (String) st.nextElement();
							String getMethod = RefMethod.gainGetMethod(str);
							o = RefMethod.invokeGetMethod(o, getMethod);
						}
					} else {
						String getMethod = RefMethod.gainGetMethod(s);
						o = RefMethod.invokeGetMethod(t, getMethod);
					}
					if (o != null) {
						idx = (o.toString()).toLowerCase().indexOf(
								text.toLowerCase());
						if (idx == 0) {
							list1.add(t);
							obj = null;
							break;
						} else if (idx > 0 && obj == null) {
							obj = t;
						}
					}
				}
				if (obj != null)
					list2.add(t);
			}
			list1.addAll(list2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list1;
	}
}
